package com.xiao.gulimall.product.service.impl;

import com.xiao.common.utils.R;
import com.xiao.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.xiao.gulimall.product.dao.AttrDao;
import com.xiao.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.xiao.gulimall.product.entity.AttrEntity;
import com.xiao.gulimall.product.entity.CategoryEntity;
import com.xiao.gulimall.product.service.AttrAttrgroupRelationService;
import com.xiao.gulimall.product.service.AttrService;
import com.xiao.gulimall.product.vo.AttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiao.common.utils.PageUtils;
import com.xiao.common.utils.Query;

import com.xiao.gulimall.product.dao.AttrGroupDao;
import com.xiao.gulimall.product.entity.AttrGroupEntity;
import com.xiao.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrDao attrDao;
    @Autowired
    AttrGroupDao attrGroupDao;
    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params,Long catId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if(catId != null && catId != 0){
            wrapper.eq("catelog_id",catId);

        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }
    @Autowired
    AttrService attrService;

//    @Override
//    public R getcatelogandwithattr(Long catelogId) {
//        //通过三级菜单 id 获取对应的所有属性分组id
//        List<AttrGroupEntity> catelog = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
//
//        List<AttrGroupVo> attr_group_id1 = catelog.stream().map((data) -> {
//            AttrGroupVo attrGroupVo = new AttrGroupVo();
//            BeanUtils.copyProperties(data, attrGroupVo);
//            // 通过属性分组id 去中间表获取对应的属性id
//            List<Long> attr_group_id = attrAttrgroupRelationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", data.getAttrGroupId())).stream().map((data1) -> {
//                return data1.getAttrId();
//            }).collect(Collectors.toList());
//            //然后通过属性id 获取对应的数据
//            if (attr_group_id.size() > 0) {
//                attrGroupVo.setAttrs((List<AttrEntity>) attrService.listByIds(attr_group_id));
//            }
//            return attrGroupVo;
//        }).collect(Collectors.toList());
//        return R.ok().put("data",attr_group_id1);
//    }

    @Override
    public List<AttrGroupVo> withattr(Long catId) {
        //1.查询到当前分类下的所有分组
        List<AttrGroupEntity> groupEntityList = baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catId));
        //groupEntityList 集合中的 每个对象都需要转换成AttrgroupVo

        List<AttrGroupVo> attrgroupVoList = groupEntityList.stream().map(item -> {
            AttrGroupVo attrgroupVo = new AttrGroupVo();
            //item 中的每个元素复制到vo中
            BeanUtils.copyProperties(item, attrgroupVo);
            //2.现在还返回vo对象 每个分组下的规格参数属性还没有查询
            Long attrGroupId = item.getAttrGroupId();
            //3.根据分组编号查询属性表
            List<AttrEntity> attrList = attrDao.selectList(new QueryWrapper<AttrEntity>().eq("attr_type", 1).eq("attr_group_id", attrGroupId));
            attrgroupVo.setAttrs(attrList);
            return attrgroupVo;
        }).collect(Collectors.toList());
        return attrgroupVoList;
    }

}